﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace IPMS.Library
{
    public class Helper
    {
        /// <summary>
        /// Lấy về đường link dựa trên action và controller
        /// </summary>
        /// <param name="action"></param>
        /// <param name="controller"></param>
        /// <param name="urlparameters"></param>
        /// <returns></returns>
        public static string GetActionLink(string action, string controller, params object[] urlparameters){
            string domain = HttpContext.Current.Request.ServerVariables["Server_Name"];
            string port = HttpContext.Current.Request.ServerVariables["Server_Port"];
            string url="http://"+domain+ ":"+ port+ "/"+controller+"/"+action;
            foreach(object parameter in urlparameters){
                url+="/"+ parameter.ToString();
            }

            return url;
        }
        public static string GetDomainURL()
        {
            string domain = HttpContext.Current.Request.ServerVariables["Server_Name"];
            string port = HttpContext.Current.Request.ServerVariables["Server_Port"];
            return "http://"+domain+ ":"+ port;
        }
        public static string GetHTML(object obj)
        {
            if(obj == null || String.IsNullOrEmpty(obj.ToString())){
                return "-";
            }
            else{
                return obj.ToString();
            }
        }
        public static string GetDate(DateTime date)
        {
            if (date != null)
            {
                return String.Format("{0:dd/MM/yyyy}", date);
            }
            else
            {
                return "-";
            }
        }
        public static string  UploadFile()
        {
                string fileName = HttpContext.Current.Request.Files[0].FileName;
            
                HttpPostedFile file = (HttpPostedFile) HttpContext.Current.Request.Files[fileName];
                string newFilename = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                string path = HttpContext.Current.Request.PhysicalApplicationPath + "uploads\\hsmt\\" + newFilename;
                file.SaveAs(path);
                return GetDomainURL() + "/upload/hsmt/" + newFilename;

            
        }
    }
}